package com.example.adsharmonyos.util;

import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

//线程切换工具类

public class HiExecutor {

    /**

     * 切换任务到主线程执行

     * @param runnable

     */

    public static void runUI(Runnable runnable){

        //切换到主线程

        EventRunner runner = EventRunner.getMainEventRunner();

        EventHandler eventHandler = new EventHandler(runner);

        //执行任务

        eventHandler.postSyncTask(runnable);

    }

    public static void runBG(Runnable runnable){

        //开启一个新的线程

        EventRunner runner = EventRunner.create(true);

        EventHandler eventHandler = new EventHandler(runner);

        //执行任务

        eventHandler.postTask(runnable, 0, EventHandler.Priority.IMMEDIATE);

    }

}